import { ElMessage } from 'element-plus'
import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '../components/Login'

const routes = [
  {
    path: '/',
    // 重定向页面
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: "/home",
    component: () => import("../components/Home"),
    // redirect:'/home/user',
    children:[
      { path:'/home',redirect:'/home/users' },
      { path:'users',component: () => import("./pages/Users") },
      { path:'roles',component: () => import("./pages/Roles") },
      { path:'rights',component: () => import("./pages/Rights") },
      { path:'goods',component: () => import("./pages/Goods") },
      { path:'params',component: () => import("./pages/Params") },
      { path:'categories',component: () => import("./pages/Categories") },
      { path:'orders',component: () => import("./pages/Orders") },
      { path:'reports',component: () => import("./pages/Reports") },
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 路由守卫
router.beforeEach((to,from,next)=>{
  if(to.path == '/login') return next();
  const tokenInfo = sessionStorage.getItem('token');
  if(!tokenInfo)  {
    ElMessage.warning('您还未登录！');
    return next('/login')
  }
  next()
})

export default router
